var widthLess970 = function() {
    if($(window).width() < 970){
        $(".table-responsive").niceScroll({
            cursorcolor: "#000",
            zindex: 999999,
            bouncescroll: true,
            cursoropacitymax: 0.4,
            cursorborder: "",
            cursorborderradius: 8,
            cursorwidth: "6px",
            background: "rgba(0,0,0,.1)",
            autohidemode: false,
            railpadding: {
                top: 10,
                right: 2,
                left: 2,
                bottom: 0
            }
        });
    }else{
        $("#ascrail2001-hr").getNiceScroll().hide();
    }
};

function PublicUrl(url){
    var str1 = $('.search-form').find('input').serialize();
    var str2 = $('.search-form').find('select').serialize();
    var query = str1+'&'+str2;
    query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
    query = query.replace(/^&/g,'');
    if( url.indexOf('?')>0 ){
        url += '&' + query;
    }else{
        url += '?' + query;
    }
    window.location.href = url;
}

$(function(){
    $(".chosen-select").chosen({disable_search_threshold: 10});

    $("#content").niceScroll({
        cursorcolor: "#000000",
        zindex: 999999,
        bouncescroll: true,
        cursoropacitymax: 0.4,
        cursorborder: "",
        cursorborderradius: 8,
        cursorwidth: "6px",
        background: "rgba(0,0,0,.1)",
        autohidemode: false,
        railpadding: {
            top: 0,
            right: 2,
            left: 2,
            bottom: 0
        }
    });

    widthLess970();
    $(window).resize(function() {
        widthLess970()
    });

    $('#allchck').change(function(){
        $(this).parents('table').find('tbody input[type="checkbox"]').prop('checked', $(this).prop('checked'));
    });

    $('#Search').click(function(){
        var url = $(this).data('url');
        PublicUrl(url);
    });

    $('.ajax-post').click(function(){
        $('.FormTable').submit();return;
        var loading;
        $.ajax({
            type: "POST",
            dataType: "json",
            url: $('.FormTable').action,
            data: $('.FormTable').serialize(),
            beforeSend:function(){
                loading = layer.load(1,{
                    shade: [0.7,'#000']
                });
            },success:function(data){
                layer.close(loading);
                if(data.Code == 1){
                    layer.msg(data.Msgs,{icon: 1,time: data.Wait});
                    if(data.Urls){
                        window.setTimeout(function(){
                            window.location.href = data.Urls;
                        },data.Wait);
                    }else{
                        window.location.reload();
                    }
                }else{
                    layer.msg(data.Msgs,{icon: 2,time: data.Wait});
                }
            },error:function(){ 
                layer.close(loading);
                layer.open({
                    icon: 7,
                    title: "系统提示",
                    content: "网络错误"
                });
            }
        });
    });

    $('.ajax-handle').click(function(){
        var loading;
        var url = $(this).data('url');
        var ids  = $(this).data('ids');
        layer.confirm("确认操作？",{
            btn: ["确认","取消"]
        },function(){
            $.ajax({
                type: "POST",
                dataType: "json",
                url: url,
                data: {id:ids},
                beforeSend:function(){
                    loading = layer.load(1,{
                        shade: [0.7,'#000']
                    });
                },success:function(data){
                    layer.close(loading);
                    layer.msg(data.Msgs,{icon: data.Code,time: data.Wait});
                    if(data.Code == 1){
                        window.setTimeout(function(){
                            window.location.reload();
                        },data.Wait);
                    }
                },error:function(){           
                    layer.close(loading);
                    layer.open({
                        icon: 7,
                        title: "系统提示",
                        content: "网络错误"
                    });
                }
            });
        });
    });

    $('.ajax-get').click(function(){
        var loading;
        var url = $(this).data('url');
        layer.confirm("确认操作？",{
            btn: ["确认","取消"]
        },function(){
            $.ajax({
                type: "POST",
                dataType: "json",
                url: url,
                beforeSend:function(){
                    loading = layer.load(1,{
                        shade: [0.7,'#000']
                    });
                },success:function(data){
                    layer.close(loading);
                    layer.msg(data.Msgs,{icon: data.Code,time: data.Wait});
                    if(data.Code == 1){
                        window.setTimeout(function(){
                            window.location.reload();
                        },data.Wait);
                    }
                },error:function(){           
                    layer.close(loading);
                    layer.open({
                        icon: 7,
                        title: "系统提示",
                        content: "网络错误"
                    });
                }
            });
        });
    });
});